package com.ctg.sysmgr.sso.server.control.view;

import java.util.Map;

import org.jasig.cas.authentication.principal.DefaultResponse;
import org.jasig.cas.authentication.principal.Response;
import org.jasig.cas.authentication.principal.Response.ResponseType;
import org.springframework.webflow.core.collection.MutableAttributeMap;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;

public class RedirectViewUrlACtion {

	/** Authentication success result. */
    public static final String SUCCESS = "success";
    
	public final Event converViewUrl(final RequestContext context) {
		MutableAttributeMap<Object> scope = context.getRequestScope();
		
		final Response response = (DefaultResponse)scope.get("response");
		
		String url = response.getUrl();
		
		String mode = context.getFlowScope().getString("mode");
		
		if(mode != null && mode.equals("checkLogin")) {
			if (url.indexOf("?") > 0) {
				url = url + "&mode=" + mode;
			} else {
				url = url + "?mode=" + mode;
			}
		}
		
		
		scope.put("redirectUrl", url);
		
		return newEvent(SUCCESS);
	}
	
	/**
     * New event based on the given id.
     *
     * @param id the id
     * @return the event
     */
    private Event newEvent(final String id) {
        return new Event(this, id);
    }
}
